package com.android.job.ui;

import java.io.IOException;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.KeyEvent;
import com.android.job.fusion.FusionField;
import com.android.job.util.CommonUtil;
import com.android.job.view.LoadingView;

/**
 * 欢迎页
 * 
 * @author
 * 
 */
public class WecomeActivity extends BaseActivity
{

	// private LoadingView loadView;
	private boolean isConnection = false;

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);

		setContentView(R.layout.welcome);
//		initViews();
		// initLoadingImages();
		// loadView.startAnim();
		FusionField.versionName = CommonUtil.getVersionName();
//		handler.sendEmptyMessageDelayed(1, 3000);
	}

	private Handler handler = new Handler()
	{

		public void handleMessage(Message msg)
		{
			switch (msg.what)
			{
			case 1:
				checkNetWork();
				break;

			default:
				break;
			}

		};

	};

//	private void initViews()
//	{
//		// loadView = (LoadingView) findViewById(R.id.welcome_load);
//	}
	
	
	
	

	private void checkNetWork()
	{
		isConnection = connectionState();

		if (isConnection)
		{
			// jumpToPage(HomeActivity.class ,null ,true);
			Intent intent = new Intent(WecomeActivity.this, HomeActivity.class);
			startActivity(intent);
			finish();
		} else
		{
			AlertDialog.Builder builder = new AlertDialog.Builder(this)
			        .setMessage("请打开网络后再试...")
			        .setTitle("网络异常")
					.setPositiveButton("设置网络", new OnClickListener()
					{

						@Override
						public void onClick(DialogInterface dialog, int which)
						{
							startActivityForResult(
									new Intent(
											android.provider.Settings.ACTION_WIRELESS_SETTINGS),
									1);
						}
					}).setNegativeButton("取消", new OnClickListener()
					{
						@Override
						public void onClick(DialogInterface dialog, int which)
						{
							finish();
						}
					});
			Dialog dialog = builder.create();
			dialog.show();
		}
	}

	private void initLoadingImages()
	{
		int[] imageIds = new int[6];

		imageIds[0] = R.drawable.loader_frame_1;
		imageIds[1] = R.drawable.loader_frame_2;
		imageIds[2] = R.drawable.loader_frame_3;
		imageIds[3] = R.drawable.loader_frame_4;
		imageIds[4] = R.drawable.loader_frame_5;
		imageIds[5] = R.drawable.loader_frame_6;

		// loadView.setImageIds(imageIds);
	}

	@Override
	protected void onDestroy()
	{
		super.onDestroy();
	}
	
	
	

	@Override
	protected void onResume()
	{
		handler.sendEmptyMessageDelayed(1, 3000);
		super.onResume();
	}

	@Override
	public boolean dispatchKeyEvent(KeyEvent event)
	{
		if (event.getAction() == KeyEvent.ACTION_DOWN)
		{
			switch (event.getKeyCode())
			{
			case KeyEvent.KEYCODE_BACK:
			{
				finish();
				try
				{
					Runtime.getRuntime().exec(
							"kill -9 " + android.os.Process.myPid());
				} catch (IOException e)
				{
					e.printStackTrace();
				}

			}

			}
		}
		return super.dispatchKeyEvent(event);
	}
}
